<template>
  <div class="ScoreManagement">
    <ScoreHeader />
    <CardHeader />
    <van-tabs v-model="active" background="transparent" @change="change">
      <van-tab
        v-for="item in PointRecordType"
        :title="item.title"
        :name="item.id"
        :key="item.id"
      >
        <van-list
          v-model="moreLoading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
          offset="50"
        >
          <van-cell-group inset class="lists">
            <van-cell v-for="item in PointRecordList" :key="item.cpointId">
              <template #title>
                <div class="item">
                  <div class="item-pic">
                    <img
                      v-if="item.gainPoint >= 0"
                      src="@/assets/wallet.png"
                      alt=""
                    />
                    <img
                      v-else-if="item.gainPoint < 0"
                      src="@/assets/gift.png"
                      alt=""
                    />
                  </div>
                  <div class="custom">
                    <span class="custom-title">{{
                      item.gainPoint >= 0 ? "获取" : "消费"
                    }}</span>
                    <div class="custom-time">{{ item.gainTime }}</div>
                  </div>
                </div>
              </template>
              <template #right-icon>
                <!-- <span class="num puls">{{item.gainPoint}}</span> -->
                <span class="num puls" v-if="item.gainPoint >= 0"
                  >+{{ item.gainPoint }}</span
                >
                <span class="num minus" v-else-if="item.gainPoint < 0">{{
                  item.gainPoint
                }}</span>
              </template>
            </van-cell>
          </van-cell-group>
        </van-list>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import ScoreHeader from "./components/header";
import CardHeader from "./components/card-header.vue";
export default {
  name: "ScoreManagement",
  components: {
    ScoreHeader,
    CardHeader,
  },
  data() {
    return {
      active: "",
      PointRecordType: [
        {
          title: "全部",
          id: "",
        },
        {
          title: "获取",
          id: "1",
        },
        {
          title: "消费",
          id: "2",
        },
      ],
      isPages: false,
      isLoading: false,
      moreLoading: false,
      finished: false,
      PointRecordList: [],
      dataValue: {
        first: 0,
        limit: 10,
        type: "",
      },
    };
  },
  created() {},
  methods: {
    change(val) {
      this.PointRecordList = [];
      this.dataValue.first = 0;
      this.dataValue.type = val;
      this.finished = false;
      this.isPages = false;
      this.onLoad("refresh");
    },
    getData(mode) {
      this.$http
        .get(
          `/api/shop/userPointRecord?first=${this.dataValue.first}&limit=${this.dataValue.limit}&type=${this.dataValue.type}`
        )
        .then((res) => {
          // this.PointRecordList = res.data;
          if (res.code == "0") {
            if (mode == "more") {
              this.PointRecordList = [...this.PointRecordList, ...res.data];
            } else {
              this.PointRecordList = res.data;
            }
            if (
              (this.PointRecordList && !this.PointRecordList.length) ||
              !res.data.length ||
              res.data.length != 10
            ) {
              this.finished = true;
              // this.isPages = true
            }
          }
          setTimeout(() => {
            this.isLoading = false;
            this.moreLoading = false;
          }, 500);
        });
    },
    onLoad(mode = "more") {
      // if (this.isPages) {
      //   return (this.finished = true);
      // }
      this.dataValue.first++;
      this.getData(mode);
    },
  },
};
</script>
<style scoped lang="less">
.lists {
  margin-top: 10px;
  .num {
    display: flex;
    align-items: center;
    font-size: 20px;
    font-weight: bold;
    &.puls {
      color: #51afb1;
    }
  }
  .minus {
    color: rgba(0, 0, 0, 0.4);
  }
  .item {
    display: flex;
    &-pic {
      img {
        width: 40px;
        height: 40px;
      }
    }
    .custom {
      flex: 1;
      padding-left: 8px;
      &-title {
        display: inline-block;
        padding-left: 4px;
        vertical-align: middle;
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-box-orient: vertical;
      }
      &-time {
        font-size: 12px;
        color: #999;
      }
    }
  }
}
</style>
